﻿using System;
using Microsoft.Win32;

namespace com.fukefu
{
    public class RegeditOperate
    {
        string name = "";
        public RegeditOperate(string name)
        {
            this.name = name;
        }
        //删除
        public void Delete(string key)
        {
            RegistryKey myKey = Registry.LocalMachine;
            myKey.DeleteSubKey(key, false);
            myKey.Close();
        }
        //写入
        public void Set(string key, string value)
        {
            RegistryKey myKey = Registry.LocalMachine;//定位。。
            myKey.CreateSubKey(@"software\tiansong\" + name);
            RegistryKey mySoft = myKey.OpenSubKey(@"software\tiansong\" + name, true);
            mySoft.SetValue(key, value);
            mySoft.Close();
            myKey.Close();
        }
        public void Set(string key, int value)
        {
            Set(key, value.ToString());
        }
        //读取
        public string GetString(string key)
        {
            string myData = "";
            RegistryKey myKey = Registry.LocalMachine;
            RegistryKey mySoft = myKey.OpenSubKey(@"software\tiansong\" + name);
            if (mySoft != null && mySoft.GetValue(key) != null)
            {
                myData = mySoft.GetValue(key).ToString();
                mySoft.Close();
            }
            myKey.Close();
            return myData;
        }
        public int GetInt(string key)
        {
            string result = GetString(key);
            if (!result.Equals(""))
            {
                return Convert.ToInt32(result);
            }
            return -1;
        }
    }
}
